package com.qf.smartplatform.service.impl;

import com.qf.smartplatform.cache.MenuCache;
import com.qf.smartplatform.constans.ResultCode;
import com.qf.smartplatform.event.MenuChangeEvent;
import com.qf.smartplatform.exceptions.DeleteDataException;
import com.qf.smartplatform.exceptions.FindDataException;
import com.qf.smartplatform.exceptions.UpdateDateException;
import com.qf.smartplatform.mapper.MenuMapper;
import com.qf.smartplatform.pojo.SysMenu;
import com.qf.smartplatform.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*** @author Tgb
 * @version V1.0
 * @Project smartplatform
 * @Package com.qf.smartplatform.service.impl
 * @Description:
 * @Date 2022/9/12 17:05
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private MenuMapper menuMapper;
    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }
    private MenuCache menuCache;
    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public void addMenu(SysMenu sysMenu) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        sysMenu.setUrl( request.getRequestURI());
        menuMapper.addMenu(sysMenu);
        context.publishEvent(new MenuChangeEvent());
    }

    @Override
    public List<SysMenu> selectAll() {
        List<SysMenu> menuList = menuCache.getList();
        return menuList;
    }

    @Override
    public SysMenu selectById(Long menuId) {
        SysMenu menu = menuCache.getById(menuId);
        Assert.notNull(menu,()->{
            throw new FindDataException("未查询到该数据", ResultCode.DATA_NOT_EXIST);
        });
        return menu;
    }

    @Override
    public int deleteById(Long menuId) {
        int result = menuMapper.deleteById(menuId);
        context.publishEvent(new MenuChangeEvent());
        Assert.isTrue(result!=0,()->{
            throw new DeleteDataException("更新失败",ResultCode.DELETE_FAIL);
        });
        return 0;
    }

    @Override
    public int updateMenu(SysMenu sysMenu) {
        SysMenu menu = menuCache.getById(sysMenu.getMenuId());
        Assert.notNull(menu,()->{
            throw new FindDataException("未查询到符合更新条件得数据",ResultCode.DATA_NOT_EXIST);
        });
        int result= menuMapper.updateMenu(sysMenu);
        context.publishEvent(new MenuChangeEvent());
        if (result==0){
            throw new UpdateDateException("更新失败",ResultCode.UPDATE_FAIL);
        }
        return 0;
    }


}
